package com.qianhui.dao;

import com.qianhui.common.CM;
import com.qianhui.common.IResult;
import com.qianhui.common.IStatus;
import com.qianhui.pojo.Buyer;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for Buyer
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see com.qianhui.pojo.Buyer
 */
public class BuyerDAO extends BaseHibernateDAO
{
    private static final Logger log = LoggerFactory.getLogger(BuyerDAO.class);
    // property constants
    public static final String BUYER_NAME = "buyerName";
    public static final String BUYER_PHONE = "buyerPhone";
    public static final String BUYER_EMAIL = "buyerEmail";
    public static final String BUYER_WECHAT = "buyerWechat";
    public static final String BUYER_QQ = "buyerQq";
    public static final String BUYER_NICKNAME = "buyerNickname";
    public static final String BUYER_PASSWORD = "buyerPassword";
    public static final String BUYER_COUNTRY = "buyerCountry";
    public static final String BUYER_PROVINCE = "buyerProvince";
    public static final String BUYER_CITY = "buyerCity";
    public static final String BUYER_HEADIMGURL = "buyerHeadimgurl";
    public static final String BUYER_SEX = "buyerSex";
    public static final String BUYER_POINTS = "buyerPoints";
    public static final String BUYER_TYPE = "buyerType";
    public static final String BUYER_REAL_NAME = "buyerRealName";
    public static final String BUYER_PEOPLE_ID = "buyerPeopleId";
    public static final String BUYER_INTRODUCER = "buyerIntroducer";
    public static final String BUYER_STATUS = "buyerStatus";

    public Buyer save(Buyer transientInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().saveOrUpdate(transientInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return transientInstance;
    }

    public List<Buyer> saveList(List<Buyer> list)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            for (Buyer temp : list)
            {
                getSession().saveOrUpdate(temp);
            }
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return list;
    }

    public int delete(Buyer persistentInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().delete(persistentInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

    public Buyer findById(java.lang.String id)
    {
        log.debug("getting Buyer instance with id: " + id);
        try
        {
            Buyer instance = (Buyer) getSession().get("com.qianhui.pojo.Buyer", id);
            return instance;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public Buyer findByBuyerWeChatId(String openid)
    {
        try
        {
            String queryString = "from Buyer b where b.buyerWechat=:buyerWechat";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter("buyerWechat", openid);
            List tempList = queryObject.list();
            if (tempList != null && tempList.size() > 0)
            {
                return (Buyer) tempList.get(0);
            }
            else
            {
                return null;
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public Buyer findByBuyerQrcodeId(int buyerQrcodeId)
    {
        try
        {
            String queryString = "from Buyer b where b.buyerQrcodeId=:buyerQrcodeId";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter("buyerQrcodeId", buyerQrcodeId);
            List tempList = queryObject.list();
            if (tempList != null && tempList.size() > 0)
            {
                return (Buyer) tempList.get(0);
            }
            else
            {
                return null;
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByProperty(String propertyName, Object value)
    {
        log.debug("finding Buyer instance with property: " + propertyName + ", value: " + value);
        try
        {
            String queryString = "from Buyer as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByBuyerName(Object buyerName)
    {
        return findByProperty(BUYER_NAME, buyerName);
    }

    public List findByBuyerPhone(Object buyerPhone)
    {
        return findByProperty(BUYER_PHONE, buyerPhone);
    }

    public List findByBuyerEmail(Object buyerEmail)
    {
        return findByProperty(BUYER_EMAIL, buyerEmail);
    }

    public List findByBuyerWechat(Object buyerWechat)
    {
        return findByProperty(BUYER_WECHAT, buyerWechat);
    }

    public List findByBuyerQq(Object buyerQq)
    {
        return findByProperty(BUYER_QQ, buyerQq);
    }

    public List findByBuyerNickname(Object buyerNickname)
    {
        return findByProperty(BUYER_NICKNAME, buyerNickname);
    }

    public List findByBuyerPassword(Object buyerPassword)
    {
        return findByProperty(BUYER_PASSWORD, buyerPassword);
    }

    public List findByBuyerCountry(Object buyerCountry)
    {
        return findByProperty(BUYER_COUNTRY, buyerCountry);
    }

    public List findByBuyerProvince(Object buyerProvince)
    {
        return findByProperty(BUYER_PROVINCE, buyerProvince);
    }

    public List findByBuyerCity(Object buyerCity)
    {
        return findByProperty(BUYER_CITY, buyerCity);
    }

    public List findByBuyerHeadimgurl(Object buyerHeadimgurl)
    {
        return findByProperty(BUYER_HEADIMGURL, buyerHeadimgurl);
    }

    public List findByBuyerSex(Object buyerSex)
    {
        return findByProperty(BUYER_SEX, buyerSex);
    }

    public List findByBuyerPoints(Object buyerPoints)
    {
        return findByProperty(BUYER_POINTS, buyerPoints);
    }

    public List findByBuyerType(Object buyerType)
    {
        return findByProperty(BUYER_TYPE, buyerType);
    }

    public List findByBuyerRealName(Object buyerRealName)
    {
        return findByProperty(BUYER_REAL_NAME, buyerRealName);
    }

    public List findByBuyerPeopleId(Object buyerPeopleId)
    {
        return findByProperty(BUYER_PEOPLE_ID, buyerPeopleId);
    }

    public List findByBuyerIntroducer(Object buyerIntroducer)
    {
        return findByProperty(BUYER_INTRODUCER, buyerIntroducer);
    }

    public List findByBuyerStatus(Object buyerStatus)
    {
        return findByProperty(BUYER_STATUS, buyerStatus);
    }

    public List findAll()
    {
        log.debug("finding all Buyer instances");
        try
        {
            String queryString = "from Buyer";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByIdList(List<String> idList)
    {
        try
        {
            String queryString = "from Buyer as model where model.buyerId in (:idList)";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameterList("idList", idList);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByIndexAndPageSizeAnd_keyword(int index, int pageSize, String keyword)
    {
        try
        {
            String sql = "from Buyer b :kSql order by b.buyerRegisterTime desc";

            if (CM.validIsEmpty(keyword) == true)
            {
                sql = sql.replace(":kSql", " ");
            }
            else
            {
                sql = sql.replace(":kSql", "where (b.buyerId like :keyword or b.buyerName like :keyword or b.buyerPhone like :keyword or b.buyerEmail like :keyword or b.buyerWechat like :keyword or b.buyerQq like :keyword or b.buyerNickname like :keyword or b.buyerRealName like :keyword)");
            }
            Query query = getSession().createQuery(sql);
            if (CM.validIsEmpty(keyword) == false)
            {
                query.setParameter("keyword", "%" + keyword + "%");
            }
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountBy_keyword(String keyword)
    {
        try
        {
            String sql = "select count(*) from Buyer b :kSql";
            if (CM.validIsEmpty(keyword) == true)
            {
                sql = sql.replace(":kSql", " ");
            }
            else
            {
                sql = sql.replace(":kSql", "where (b.buyerId like :keyword or b.buyerName like :keyword or b.buyerPhone like :keyword or b.buyerEmail like :keyword or b.buyerWechat like :keyword or b.buyerQq like :keyword or b.buyerNickname like :keyword or b.buyerRealName like :keyword)");
            }
            Query query = getSession().createQuery(sql);
            if (CM.validIsEmpty(keyword) == false)
            {
                query.setParameter("keyword", "%" + keyword + "%");
            }
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByIndexAndPageSizeAndBuyerIntroducer(int index, int pageSize, String buyerIntroducer)
    {
        try
        {
            String sql = "from Buyer b where b.buyerIntroducer=:buyerIntroducer order by b.buyerRegisterTime desc";

            Query query = getSession().createQuery(sql);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            query.setParameter("buyerIntroducer", buyerIntroducer);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByBuyerIntroducer(String buyerIntroducer)
    {
        try
        {
            String sql = "select count(*) from Buyer b where b.buyerIntroducer=:buyerIntroducer";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerIntroducer", buyerIntroducer);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByIndexAndPageSizeAndStatusAnd_keyword(int index, int pageSize, String status, String keyword)
    {
        try
        {
            String sql = "from Buyer b where b.buyerStatus=:status :kSql order by b.buyerRegisterTime desc";
            if (CM.validIsEmpty(keyword) == true)
            {
                sql = sql.replace(":kSql", " ");
            }
            else
            {
                sql = sql.replace(":kSql", "and (b.buyerId like :keyword or b.buyerName like :keyword or b.buyerPhone like :keyword or b.buyerEmail like :keyword or b.buyerWechat like :keyword or b.buyerQq like :keyword or b.buyerNickname like :keyword or b.buyerRealName like :keyword)");
            }
            Query query = getSession().createQuery(sql);
            query.setParameter("status", status);
            if (CM.validIsEmpty(keyword) == false)
            {
                query.setParameter("keyword", "%" + keyword + "%");
            }
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByStatusAnd_keyword(String status, String keyword)
    {
        try
        {
            String sql = "select count(*) from Buyer b where b.buyerStatus=:status :kSql";
            if (CM.validIsEmpty(keyword) == true)
            {
                sql = sql.replace(":kSql", " ");
            }
            else
            {
                sql = sql.replace(":kSql", "and (b.buyerId like :keyword or b.buyerName like :keyword or b.buyerPhone like :keyword or b.buyerEmail like :keyword or b.buyerWechat like :keyword or b.buyerQq like :keyword or b.buyerNickname like :keyword or b.buyerRealName like :keyword)");
            }
            Query query = getSession().createQuery(sql);
            query.setParameter("status", status);
            if (CM.validIsEmpty(keyword) == false)
            {
                query.setParameter("keyword", "%" + keyword + "%");
            }
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByIndexAndPageSizeAndStatusAndTypeList_keyword(int index, int pageSize, String status, List<String> typeList, String keyword)
    {
        try
        {
            String sql = "from Buyer b where b.buyerStatus=:status and b.buyerType in (:typeList) :kSql order by b.buyerRegisterTime desc";
            if (CM.validIsEmpty(keyword) == true)
            {
                sql = sql.replace(":kSql", " ");
            }
            else
            {
                sql = sql.replace(":kSql", "and (b.buyerId like :keyword or b.buyerName like :keyword or b.buyerPhone like :keyword or b.buyerEmail like :keyword or b.buyerWechat like :keyword or b.buyerQq like :keyword or b.buyerNickname like :keyword or b.buyerRealName like :keyword)");
            }
            Query query = getSession().createQuery(sql);
            query.setParameter("status", status);
            query.setParameterList("typeList", typeList);
            if (CM.validIsEmpty(keyword) == false)
            {
                query.setParameter("keyword", "%" + keyword + "%");
            }
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByStatusAndTypeList_keyword(String status, List<String> typeList, String keyword)
    {
        try
        {
            String sql = "select count(*) from Buyer b where b.buyerStatus=:status and b.buyerType in (:typeList) :kSql";
            if (CM.validIsEmpty(keyword) == true)
            {
                sql = sql.replace(":kSql", " ");
            }
            else
            {
                sql = sql.replace(":kSql", "and (b.buyerId like :keyword or b.buyerName like :keyword or b.buyerPhone like :keyword or b.buyerEmail like :keyword or b.buyerWechat like :keyword or b.buyerQq like :keyword or b.buyerNickname like :keyword or b.buyerRealName like :keyword)");
            }
            Query query = getSession().createQuery(sql);
            query.setParameter("status", status);
            query.setParameterList("typeList", typeList);
            if (CM.validIsEmpty(keyword) == false)
            {
                query.setParameter("keyword", "%" + keyword + "%");
            }
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByIndexAndPageSizeAndBuyerIntroducerAndNotPuTongBuyer(int index, int pageSize, String buyerIntroducer)
    {
        try
        {
            String sql = "from Buyer b where b.buyerIntroducer=:buyerIntroducer and b.buyerType is not null and b.buyerType !=:puTongBuyer order by b.buyerRegisterTime desc";

            Query query = getSession().createQuery(sql);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            query.setParameter("buyerIntroducer", buyerIntroducer);
            query.setParameter("puTongBuyer", IStatus.BUYER_TYPE_PLAIN_BUYER);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByBuyerIntroducerAndNotPuTongBuyer(String buyerIntroducer)
    {
        try
        {
            String sql = "select count(*) from Buyer b where b.buyerIntroducer=:buyerIntroducer and b.buyerType is not null and b.buyerType !=:puTongBuyer";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerIntroducer", buyerIntroducer);
            query.setParameter("puTongBuyer", IStatus.BUYER_TYPE_PLAIN_BUYER);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByIndexAndPageSizeAndBeginTimeAndEndTime(int index, int pageSize, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "from Buyer b where b.buyerRegisterTime between :beginTime and :endTime order by b.buyerRegisterTime asc";

            Query query = getSession().createQuery(sql);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);

            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByBeginTimeAndEndTime(Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select count(*) from Buyer b where b.buyerRegisterTime between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

}